package me.shurufa.holder;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.event.EventObject;
import me.shurufa.implement.DigestLongClickListener;
import me.shurufa.model.Digest;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 针对我的书摘展现holder
 *
 * 为什么不复用的原因
 * 1.getItemType太麻烦
 * 2.bind方法中使用判断uid隐藏或显示 布局太多 效率不高
 * 3.方便下版本增删布局和逻辑
 *
 * @FileName: me.shurufa.holder.MyMarkViewHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-02-04 19:09
 */
public class MyMarkViewHolder extends BaseViewHolder implements View.OnLongClickListener {

    private Context mContext;
    private Digest mDigest;
    @Bind(R.id.mark_digest)
    TextView markDigest;
    @Bind(R.id.mark_note)
    TextView markNote;
    @Bind(R.id.mark_from)
    TextView markFrom;
    @Bind(R.id.mark_edit)
    ImageView markEdit;
    @Bind(R.id.digest_root)
    RelativeLayout digestRoot;
    @Bind(R.id.publish_state)
    TextView publishState;

    //mark from
    String date;
    String pageNumberFrom;
    String partName;

    private DigestLongClickListener listener;
    SpannableString myNote;
    public MyMarkViewHolder(View itemView, Object parent,DigestLongClickListener listener) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        addOnClickListener(markEdit);
        this.listener = listener;
        if (listener!=null)
            digestRoot.setOnLongClickListener(this);
        myNote = new SpannableString(mContext.getString(R.string.note_of_mine));
        myNote.setSpan(new ForegroundColorSpan(Color.parseColor("#d70c17")),0,myNote.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }

    public void bind(Digest digest){
        if (digest==null)
            return;
        mDigest = digest;
        if (!TextUtils.isEmpty(digest.publish_state))
            publishState.setText(checkState(digest.publish_state));
        else
            publishState.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(digest.content))
            markDigest.setText(digest.content);
        else
            markDigest.setText("");
        if (!TextUtils.isEmpty(digest.note)) {
            markNote.setText(TextUtils.concat(myNote,digest.note));
        }else {
            markNote.setText(myNote);
        }

        setMarkFrom(digest);

    }

    private String checkState(String publish_state) {
        if (publish_state.equals("publish"))
            return mContext.getString(R.string.has_published);
        else
            return mContext.getString(R.string.not_publish);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.mark_edit:
                EventObject object = new EventObject();
                object.setEventAction(Constants.ACTION_EDIT_MY_DIGEST);
                object.setDigest(mDigest);
                EventBus.getDefault().post(object);
                break;
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (listener!=null){
            listener.onDigestLongClick(v,mDigest);
        }
        return true;
    }

    private void setMarkFrom(Digest digest){

        if (!TextUtils.isEmpty(digest.created_at))
            date = Utils.tranformDate(digest.created_at)+" ";
        else  date="";

        if (digest.menu_info!=null)
            partName = "\n"+String.format(mContext.getString(R.string.name_of_part),digest.menu_info.title);
        else partName = "\n"+mContext.getString(R.string.no_part);


        if (digest.page_num!=0)
            pageNumberFrom = String.format(mContext.getString(R.string.page_of_book),String.valueOf(digest.page_num));
        else pageNumberFrom = "";

        SpannableString span = new SpannableString(partName);
        span.setSpan(new RelativeSizeSpan(0.7f), 0, span.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        markFrom.setText(TextUtils.concat(date,pageNumberFrom,span));

    }
}
